<?php


namespace app\common\model;

use think\Db;


class News{
    public function getAllUpNewsList(){
        $sql = 'select news.*, user.username from news left join user on user.uid = news.admin_id where status = 1 order by ntime desc';
        return Db::query($sql);
    }




    public function getPageUpNewsList($from, $length){
        $sql = "select news.*, user.username from news left join user on user.uid = news.admin_id where status = 1 order by ntime desc limit :from, :length";
        return Db::query($sql,['from'=>$from,'length'=>$length]);
    }

    public function getUpNewsListCount(){
        $sql = "select COUNT(*) as totalCount from news where status = 1 ";
        return Db::query($sql);
    }




    public function getAllRecommendNewsList(){
        $sql = 'select nid,title,ntime,imgurl from news where (recommend = 1 and status = 1) ORDER BY ntime desc';
        return Db::query($sql);
    }

    public function getNewsById($nid){
        $sql = 'select n.*,u.username from news n LEFT JOIN user u on u.uid = n.admin_id where n.nid = :nid';
        return Db::query($sql,['nid'=>$nid]);
    }

    public function getAllNewsList(){
        $sql = 'select news.*, user.username from news left join user on user.uid = news.admin_id order by ntime desc';

        return Db::query($sql);
    }

    public function add($title, $remark, $detail, $adminId, $imgurl, $recommend){
        $sql = 'insert into news (title, remark, detail, admin_id, imgurl, recommend) VALUE (:title, :remark, :detail, :adminId, :imgurl, :recommend) ';
        return Db::query($sql,['adminId'=>$adminId,'title'=>$title, 'remark'=>$remark,'detail'=>$detail,'imgurl'=>$imgurl,'recommend'=>$recommend]);
    }

    public function edit($nid, $title, $remark, $detail, $imgurl, $recommend){
        $sql = "update news set title = :title, remark = :remark, detail = :detail, imgurl = :imgurl, recommend = :recommend where nid = :nid";

        return Db::query($sql,['nid'=>$nid,'title'=>$title, 'remark'=>$remark,'detail'=>$detail,'imgurl'=>$imgurl,'recommend'=>$recommend]);
    }

    public function deleteNews($nid){
        $sql = "delete from news where nid = :nid";

        return Db::query($sql,['nid'=>$nid]);
    }

    public function status($nid, $status){
        $sql = "update news set status = :status where nid = :nid";

        return Db::query($sql,['nid'=>$nid,'status'=>$status]);
    }


}